import 'dart:io';

import 'package:cached_network_image/cached_network_image.dart';
import 'package:fluro/fluro.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:flutter_swiper/flutter_swiper.dart';
import 'package:ugrownews_flutter/main/ScreenNewsDetail.dart';
import 'package:ugrownews_flutter/utils/RouterUtil.dart';
import 'package:ugrownews_flutter/utils/netutil/NetUtil.dart';

import 'dart:convert';

import 'package:ugrownews_flutter/utils/netutil/NewsNetUtil.dart';

class MainBannerSwiper extends StatefulWidget {
  @override
  _MainBannerSwiperState createState() {
    _MainBannerSwiperState _mainBannerSwiperState =
        new _MainBannerSwiperState();
    _initState(_mainBannerSwiperState);
    return _mainBannerSwiperState;
  }
}

void _initState(_MainBannerSwiperState mainBannerSwiperState) {
  Map<String, String> params = new Map();
  params["page"] = "1";
  params["pageSize"] = "10";
  params["id"] = "46";
  new NewsNetUtil().getNewsList(params, (result, isCache) {
    if (result is NetError) {
      return;
    } else {
      Map jsonResult = json.decode(result);
      String state = jsonResult["state"];
      if (state.isNotEmpty && state == "success") {
        if (mainBannerSwiperState.mounted) {
          new Future.delayed(Duration(seconds: 0), () {
            mainBannerSwiperState.setState(() {
              mainBannerSwiperState.autoplay = true;
              mainBannerSwiperState.data = jsonResult["data"];
            });
          });
        }
      }
    }
  });
}

class _MainBannerSwiperState extends State<MainBannerSwiper>
    with AutomaticKeepAliveClientMixin {
  List<dynamic> data = new List();
  bool autoplay = false;
  List<Widget> bannerList = new List();

  @override
  Widget build(BuildContext context) {
    super.build(context);
    return new Swiper(
      itemWidth: ScreenUtil.screenWidthDp,
      itemHeight: ScreenUtil.screenHeightDp,
      itemBuilder: _swiperBuilder,
      itemCount: data.length,
      //关闭无限循环以减少滑动返回卡顿
      loop: true,
      pagination: new SwiperPagination(
        margin:
            new EdgeInsets.only(bottom: ScreenUtil.getInstance().setWidth(20)),
        builder: DotSwiperPaginationBuilder(
          color: Colors.grey,
          size: ScreenUtil.getInstance().setWidth(10),
          activeSize: ScreenUtil.getInstance().setWidth(10),
          activeColor: Color.fromARGB(0xff, 0x17, 0x00, 0xE6),
        ),
      ),
      scrollDirection: Axis.horizontal,
      autoplay: autoplay,
    );
  }

  buildWidget(BuildContext c, int i) {}

  Widget _swiperBuilder(BuildContext c, int i) {
    return new Container(
      height: ScreenUtil.getInstance().setWidth(608),
      width: ScreenUtil.screenWidthDp,
      child: new GestureDetector(
        child: new Stack(
          children: <Widget>[
            new CachedNetworkImage(
              imageUrl: data[i]["customImage"],
              placeholder: (context, url) {
                return new Image.asset(
                  "images/image_loading1.png",
                  height: ScreenUtil.getInstance().setWidth(608),
                  width: ScreenUtil.screenWidthDp,
                  fit: BoxFit.fill,
                );
              },
              errorWidget: (context, url, builder) {
                return new Icon(Icons.error);
              },
              height: ScreenUtil.getInstance().setWidth(608),
              width: ScreenUtil.screenWidthDp,
              fit: BoxFit.fill,
            ),
            new Positioned(
              bottom: 0,
              child: new Image.asset(
                "images/bannerview_back.png",
                fit: BoxFit.fill,
                width: ScreenUtil.screenWidthDp,
                height: ScreenUtil.getInstance().setWidth(220),
              ),
            ),
            new Positioned(
              bottom: ScreenUtil.getInstance().setWidth(46),
              left: ScreenUtil.getInstance().setWidth(30),
              right: ScreenUtil.getInstance().setWidth(30),
              child: new Text(
                data[i]["title"],
                overflow: TextOverflow.ellipsis,
                softWrap: true,
                style: TextStyle(
                  color: Colors.white,
                  fontSize: ScreenUtil.getInstance().setWidth(46),
                ),
                maxLines: 2,
              ),
            ),
          ],
        ),
        onTap: () {
          RouterUtil.getInstance().navigateTo(context,
              RouterUtil.ROUTER_NEWSDETAIL + "?id=" + data[i]["id"].toString(),
              transition: TransitionType.cupertino);
        },
      ),
    );
  }

  @override
  bool get wantKeepAlive => true;
}
